<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/13 0013
 * Time: 10:39
 * 产品溯源信息
 */
defined("IN_IA")or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "goods_trace_type";

//溯源列表
if($op=='goods_trace_type'){
    $all=pdo_getall('cqkundian_farm_goods_trace_type',array('uniacid'=>$uniacid));
    $count=count($all);
    $pageIndex=$_GPC['page'] ? $_GPC['page'] :1;
    $pageSize=10;
    $pager=pagination($count,$pageIndex,$pageSize);
    $list=pdo_getall('cqkundian_farm_goods_trace_type',array('uniacid'=>$uniacid),'','','rank asc',array($pageIndex,$pageSize));
    include $this->template('web/trace/goods_trace_type');
}

//溯源分类编辑
if($op=='goods_trace_type_edit'){
    if(!empty($_GPC['id'])){
        $list=pdo_get('cqkundian_farm_goods_trace_type',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    }
    include $this->template('web/trace/goods_trace_type_edit');
}

//保存溯源分类信息
if($op=='goods_trace_type_edit_save'){
    $data=array(
        'name'=>$_GPC['name'],
        'uniacid'=>$uniacid,
        'rank'=>$_GPC['rank'],
    );
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_goods_trace_type',$data);
    }else{
        $res=pdo_update('cqkundian_farm_goods_trace_type',$data,array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    }
    if($res){
        message('操作成功',$this->createWebUrl('trace'));die;
    }else{
        message('操作失败');die;
    }
}

//删除溯源分类信息
if($op=='goods_trace_type_delete'){
    pdo_delete('cqkundian_farm_goods_trace',array('uniacid'=>$uniacid,'trace_id'=>$_GPC['id']));
    $res=pdo_delete('cqkundian_farm_goods_trace_type',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));

    echo $res ? json_encode(array('status'=>1)) : json_encode(array('code'=>2));die;
}

//产品溯源列表
if($op=='trace_list'){
    $trace_id=$_GPC['id'];
    $list=pdo_getall('cqkundian_farm_goods_trace',array('uniacid'=>$uniacid,'trace_id'=>$trace_id),'','','rank asc');
    include $this->template('web/trace/trace_list');
}
if($op=='trace_edit'){
    if(empty($_GPC['id'])){
        $trace_id=$_GPC['trace_id'];
    }else{
        $list=pdo_get('cqkundian_farm_goods_trace',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
        $list['img']=unserialize($list['img']);
        $trace_id=$list['trace_id'];
    }

    include $this->template('web/trace/trace_edit');
}


//溯源信息保存
if($op=='trace_edit_save'){
    $data=array(
        'trace_name'=>$_GPC['trace_name'],
        'trace_desc'=>$_GPC['trace_desc'],
        'trace_time'=>strtotime($_GPC['trace_time']),
        'uniacid'=>$uniacid,
        'create_time'=>time(),
        'rank'=>$_GPC['rank'],
        'trace_id'=>$_GPC['trace_id'],
    );
    $img=$_GPC['img'];
    for ($i=0;$i<count($img);$i++){
        $img[$i]=toimage($img[$i]);
    }
    $data['img']=serialize($img);
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_goods_trace',$data);
    }else{
        $res=pdo_update('cqkundian_farm_goods_trace',$data,array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    }
    if($res){
        message('操作成功',url('site/entry/trace',array('m'=>'kundian_farm','id'=>$_GPC['trace_id'],'op'=>'trace_list')));die;
    }else{
        message('操作失败');die;
    }
}

//删除溯源信息
if($op=='trace_delete'){
    $res=pdo_delete('cqkundian_farm_goods_trace',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('code'=>2));die;
}
